﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data.Linq;
using System.Linq.Expressions;
using System.Linq;
using System.Xml.Linq;
using System.Data.Linq.Mapping;
using System.Data.Linq.Provider;
using YakShaver.WCSF.BuildManager.Interface.Services;
using YakShaver.WCSF.BuildManager.Interface.BusinessEntities;
using Microsoft.Practices.CompositeWeb.ObjectBuilder.Strategies;
using Microsoft.Practices.ObjectBuilder;
namespace YakShaver.WCSF.BuildManager.Services
{
    public class BuildService : IProjectService
    {
        #region IBuildService Members

        private string _serverUrl = "tcp://localhost:21235/CruiseManager.rem";
        private ThoughtWorks.CruiseControl.Remote.ICruiseManagerFactory cruiseFactory;
        private ThoughtWorks.CruiseControl.Remote.ICruiseManager cruiseManager;
        [InjectionConstructor]
        public BuildService()
        {
            cruiseFactory = new ThoughtWorks.CruiseControl.Remote.RemoteCruiseManagerFactory();
            cruiseManager = cruiseFactory.GetCruiseManager(_serverUrl);
        }
        
        public BuildService(string serverUrl)
        {
            _serverUrl = serverUrl;
            cruiseFactory = new ThoughtWorks.CruiseControl.Remote.RemoteCruiseManagerFactory();
            cruiseManager = cruiseFactory.GetCruiseManager(_serverUrl);
           
        }

        public IList<IProject> GetProjects()
        {
            // get the builds avail from the cruise server

          
            List<IProject> builds = new List<IProject>();
            ThoughtWorks.CruiseControl.Remote.ProjectStatus[] projectStati = cruiseManager.GetProjectStatus();
            
                      foreach (ThoughtWorks.CruiseControl.Remote.ProjectStatus status in projectStati)
           {
               BuildInfo buildInfo = new BuildInfo(status.Name);
           
               
                builds.Add(buildInfo);
           }
           
            return (IList<IProject>) builds;

        }

       

        public bool DestroySandbox(IProjectSandbox sandbox)
        {

            return true;
        }

        #endregion

        #region IBuildService Members


        public IProjectSandbox CreateSandbox(IProject build)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IProjectService Members


        public IList<IProjectBuild> GetProjectBuilds(string projectBuildId)
        {
            string[] projectBuilds = cruiseManager.GetBuildNames(projectBuildId);
            IList<IProjectBuild> yakProjectBuilds = new List<IProjectBuild>();
           

            string projectXml = cruiseManager.GetProject(projectBuildId)        ;
            IProject project = new YakShaver.WCSF.BuildManager.BuildInfo(projectBuildId);
                               
            foreach(string projectLogFileName in projectBuilds) 
            {
                ThoughtWorks.CruiseControl.Core.LogFile logFile = 
                    new ThoughtWorks.CruiseControl.Core.LogFile(projectLogFileName);

                if (logFile.Succeeded)
                {
                 
                    
                    IProjectBuild yakProjectBuild = new ProjectBuild(logFile.Label, logFile.Date,project);
                    yakProjectBuilds.Add(yakProjectBuild);
                }
            }
          
            return yakProjectBuilds;
        }

        #endregion

        #region IProjectService Members


        public string GetProjectBuildSourceDirectory(IProjectBuild projectBuild)
        {
            string project = cruiseManager.GetProject(projectBuild.Project.BuildName);

            System.Xml.XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(project);

            string publishDir = xDoc.SelectSingleNode(@"/project/publishers/buildpublisher/publishDir").InnerText;
            publishDir += @"\" + projectBuild.Label;
         
            return publishDir;
        }

        #endregion
    }
}
